package com.huluobo.day11_im.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.huluobo.day11_im.IMApp;
import com.huluobo.day11_im.R;
import com.huluobo.day11_im.activity.ChatActivity;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.modules.contact.EaseContactListFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ContactFragment extends EaseContactListFragment {
    private List<String> usernames = new ArrayList<>();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EMClient.getInstance().contactManager().aysncGetAllContactsFromServer(new EMValueCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {
                Collections.sort(value);
                usernames.addAll(value);
                Log.i("TagA", "usernames:" + usernames);
            }

            @Override
            public void onError(int error, String errorMsg) {
                Log.i("TagA", "errorMsg:" + errorMsg);
            }
        });


    }

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        if (usernames.size() != 0) {
            String conversationId = usernames.get(position);
            Intent intent = new Intent(getActivity(), ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(IMApp.CHAT_CONVERSATION_ID, conversationId);
            bundle.putInt(IMApp.CHAT_TYPE, IMApp.CHAT_TYPE_SINGLE);
            intent.putExtras(bundle);
            getActivity().startActivity(intent);
        }
    }
}